/*
 * AddressManagerListItem.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.UserAddress;
import com.yidiandao.business.data.model.AddressModel;
import com.yidiandao.business.data.model.IAddressModel;
import com.yidiandao.business.manager.UserAddressManager;
import com.yidiandao.business.ui.addressabout.editaddress.EditAddressActivity;
import com.yidiandao.business.ui.widget.dialog.BusinessDialog;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.ChooseAddressManagerHolder;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

/**
 * 选择地址ListItem.
 */
public class AddressManagerListItem extends DisplayListItem<ChooseAddressManagerHolder, UserAddress>
        implements View.OnClickListener {

    private Activity mActivity;

    private LoadingDialog mLoadingDialog;

    private IAddressModel mAddressModel;

    public AddressManagerListItem(UserAddress s) {
        super(s);
        mAddressModel = new AddressModel();
    }

    @Override
    public void onShow(final Context context, final ChooseAddressManagerHolder holder) {
        mActivity = (Activity) context;

        mLoadingDialog = new LoadingDialog(context);

        holder.refreshUI(data);
        holder.mLlytSetDefault.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 必须选中一个
                        data.setDefault(true);
                        holder.mCbChoose.setChecked(true);

                        UserAddressManager.getInstance().setDefaultAddress(data);
                        mAddressModel.postRefreshAddressStateEvent();

                        // 设置默认地址
                        mLoadingDialog.show();
                        mAddressModel.setDefaultAddress(data.getAddressId(), getDisplayItemTag(),
                                new IAddressModel.OnAccessNetListener<Object>() {
                                    @Override
                                    public void onSuccess(Object o) {
                                        mLoadingDialog.dismiss();
                                        UIUtils.showToast(UIUtils.getString(R.string.set_success));
                                        mAddressModel.postRefreshAddressListEvent();
                                    }

                                    @Override
                                    public void onFailed() {
                                        mLoadingDialog.dismiss();
                                    }
                                });
                    }
                });

        holder.mTvEdit.setOnClickListener(this);
        holder.mTvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UmengUtils.onEvent(mActivity, R.string.umeng_user_delete_address);
                showDeleteHintDialog(holder);
            }
        });
    }

    public void showDeleteHintDialog(ChooseAddressManagerHolder holder) {

        if (holder.isDefaultAddress()) {
            UIUtils.showToast(
                    UIUtils.getString(R.string.address_manager_default_address_cannot_delete));
            return;
        }

        new BusinessDialog.Builder(mActivity)
                .setTitle(R.string.choose_address_delete_hint)
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(
                        R.string.confirm,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // 删除地址
                                mLoadingDialog.show();
                                deleteUserAddress();
                            }
                        })
                .create()
                .show();
    }

    private void deleteUserAddress() {
        mAddressModel.deleteUserAddress(
                data.getAddressId(),
                getDisplayItemTag(),
                new IAddressModel.OnAccessNetListener<Object>() {
                    @Override
                    public void onSuccess(Object o) {
                        mLoadingDialog.dismiss();
                        UIUtils.showToast(UIUtils.getString(R.string.delete_success));
                        mAddressModel.postRefreshAddressListEvent();
                        // 如果用户删除的地址与保存的选中的地址相同，则需要从UserAddressManager中删除掉
                        if (data.equals(
                                UserAddressManager.getInstance().getSavedSelectedUserAddress())) {
                            UserAddressManager.getInstance().clearSavedUserAddress();
                        }
                    }

                    @Override
                    public void onFailed() {
                        mLoadingDialog.dismiss();
                    }
                });
    }

    @Override
    public String getDisplayItemTag() {
        return AddressManagerListItem.class.getSimpleName();
    }

    @Override
    public Class<ChooseAddressManagerHolder> getViewHolderClazz() {
        return ChooseAddressManagerHolder.class;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_edit:
                EditAddressActivity.actionStart(mActivity, data);
                break;
        }
    }
}
